<?php
//------------------------------------------------------------------------------------------------------------------------------
//	Nom : lib_read.php
//	Auteurs: Defondemieres Fabrice - Delfour Kevin
//	Description :
//------------------------------------------------------------------------------------------------------------------------------

//Gestion des erreurs
//ini_set("error_reporting", E_ALL);
//ini_set("display_errors","0"); 
//ini_set("ignore_repeated_errors","0");
//ini_set("log_errors", "0" ); 
//ini_set("error_log", "errors/errors_lib_read.txt" ); 

include ('Lib_Anote.php');

//Recupere le texte stocké dans le fichier et le transforme sous forme de tableau
function file2array ($target){
	$i=0;
	$tampon='';
	$fp =  fopen("data/art/$target.txt","r");
	while (!feof ($fp))
	{
		$tampon = fgets($fp);
		$tampon = nl2br($tampon);
		if ($tampon !=''){
			$article2array [$i] = $tampon;
			$i = $i + 1;
		}
		$tampon = '';
	}
	fclose($fp);
	return $article2array;
}

//Parcour d'un tableau pour le transformer au format html
function array2html ($array){
	$article ='';
	for ($i=0; $i != sizeOf($array); $i++){
		$article .= $array[$i];
	}
	$article = stripslashes($article);
	return $article;
}

//Parcour d'un tableau d'article pour faire des liens vers des articles externes
function txt2link ($string){
	links("data/art");
	$liste = $GLOBALS['liste_articles'];
	for ($i=0; $i != sizeOf($liste); $i++){
		$tmp = $liste[$i];
		$string = str_replace("$tmp", "<a href='article.php?article=$tmp'>$tmp</a>",$string);
		$tmp = strtolower($tmp);
		$string = txtreplace ($string,$tmp," ");
		$string = txtreplace ($string,$tmp,"s ");
		$string = txtreplace ($string,$tmp,".");
		$string = txtreplace ($string,$tmp,",");
	}
	return $string;
}

function txtreplace ($string,$racine,$caractere){
	$racine .= $caractere;
	$string = str_replace("$racine", "<a href='article.php?article=$racine'>$racine</a>",$string);
	return $string;
}

//Recupere le tableau d'annotation pour les transformer en infobulle
function note2tooltip($target,$string){
	$array = read_note ($target);
	$link = NULL;
	$links = "";
	$texte="";
	if (file_exists("data/note/$target.txt")) {
				for ($i=0; $i != sizeOf($array); $i++){
				$links = "";
				$num = $array[$i][0];
				$texte = $array[$i][1];
				$note =  $array[$i][2];
				if($array[$i][3] != NULL){
					$list = $array[$i][3];
					$link = split("::",$list);
					$links = array2links ($array,$link);
				}
				$textes[$i] = "$texte";
				$bulle [$i] = txt2bulle($target,$texte,$note,$links,$num);
			}
			$string = str_replace($textes,$bulle,$string);
			
			for ($i=0;$i!=sizeOf($array);$i++){
				$num_id = $array[$i][0];
				$num_id++;$num_id--;
				$texte2change = " [idnote$num_id] ";
				$string = str_replace($texte2change,$textes[$i],$string);
			}	
	}
	return $string;	
}

function note2left($target){
	$array = read_note ($target);
	$texte = "";	
  $link = NULL;
	$links = "";
	$texte="";
	$string = "";
	if (file_exists("data/note/$target.txt")) {
				for ($i=0; $i != sizeOf($array); $i++){
				$links = "";
				$num = $array[$i][0];
				$texte = $array[$i][1];
				$note =  $array[$i][2];
				if($array[$i][3] != NULL){
					$list = $array[$i][3];
					$link = split("::",$list);
					$links = array2links ($array,$link);
				}
				$textes[$i] = "$texte";
				$bulle [$i] = txt2txt($target,$texte,$note,$links,$num);
			}
			$string = str_replace($textes,$bulle,$string);
			
			for ($i=0;$i!=sizeOf($array);$i++){
				$num_id = $array[$i][0];
				$num_id++;$num_id--;
				$texte2change = " [idnote$num_id] ";
				$string = str_replace($texte2change,$textes[$i],$string);
			}	
	}
	return $string;	
}

//Mise en page d'un infobulle
function txt2txt($target,$texte,$noted,$links,$num){
	$note = "<div class=\"post\"><b>Related links :</b>$links</div>";
	$link_edit = "javascript:OuvrirFenetre('note.php?article=$target&texte=$texte&note=$noted&num=$num','nom','toolbar=0,menubar=0,location=0,border=0,scrollbars=0,width=600,height=600')";
	$bulle= "<a href=\"$link_edit\">$texte<span>$note</span></a>";
	return $bulle;
}

function txt2bulle($target,$texte,$noted,$links,$num){
	$note = "<div class=\"post\"><b>Note :</b>&nbsp;<i>$noted</i><br/><b>Related links :</b>$links</div>";
	$link_edit = "javascript:OuvrirFenetre('note.php?article=$target&texte=$texte&note=$noted&num=$num','nom','toolbar=0,menubar=0,location=0,border=0,scrollbars=0,width=600,height=600')";
	$bulle= "<a class='info' href=\"$link_edit\">$texte<span>$note</span></a>";
	return $bulle;
}

//Recupere les liens vers les differentes annotations
function array2links ($base,$link){
	$links ="";
	for ($i=0; $i != sizeOf($link); $i++){
		$num_s = $link[$i];
		if ($num_s != "") {
			$links .= return_note($num_s,$base);
		}
	}
	return $links;
}

//Recupere les informations sur l'annotation $num
function return_note ($num,$array){
	$string ="";
	for ($k=0; $k != sizeOf($array); $k++){
		$num_c = $array[$k][0];
		$num_c++;$num_c--;
		if ($num == $num_c){
			$select = $array[$k][1];
			$not = $array[$k][2];
			$string = "<br/> [idnote$num_c] ";
		}
	}
	return $string;
}

//Fonction permettant de mettre en page un article
function read_article ($target){
	$array = file2array($target);
	$string = array2html ($array);
	$string = txt2link ($string);
	$string = convertSymbols2Entities ($string);
		$string = Code2String($string);
		if (file_exists("data/note/$target.txt")) {
			$string = note2tooltip ($target,$string);
		}
	$tampon ="<div class=\"post\" onmouseup='getSelectedText();'><h1>$target</h1><p>$string</p><hr/></div>";
	return $tampon;
}

function read_txt ($target){
	$array = file2array($target);
	$string = array2html ($array);
	$string = strip_tags($string);
		
 	if (file_exists("data/note/$target.txt")) {
		 $string = note2code ($target,$string);
	} 
	
	return $string;
}

//Fonction permettant d'extraire une partie de l'article
function extrait_article($target){
	$tampon='';
		$fp =  fopen("data/art/$target.txt","r");
		$octet = filesize("data/art/$target.txt");
		if ($octet==0){$tampon.= "ATTENTION ! Fichier vide.";}
		else {
			$tampon .= fgets($fp,256);
			$tampon .="...<br/>";
		}
		return $tampon;
}

//Fonction permettant de lire le nombre d'article
function nb_articles (){
	$nbFile = 0;
	$nbRep = 0;
	$nom_repertoire = "data/art/";
	$pointeur = opendir($nom_repertoire);
	while ($fichier = readdir($pointeur)){
		if(($fichier != '.') && ($fichier != '..')){
			if (is_dir($nom_repertoire.'/'.$fichier)){
				lister($nom_repertoire.'/'.$fichier);
				$nbRep = $nbRep + 1;
			}
			else{
				$nbFile = $nbFile + 1;
			}
		}
	}
	return $nbFile;
}

function links($chemin){  
   $nom_repertoire = $chemin;
   $pointeur = opendir($nom_repertoire);
   while ($fichier = readdir($pointeur)){
	if(($fichier != '.') && ($fichier != '..')){
			if (is_dir($nom_repertoire.'/'.$fichier)){
				links($nom_repertoire.'/'.$fichier);
			}
			else{
				$fichier_d = decorticage ($fichier);
				$GLOBALS['liste_articles'][] = $fichier_d;
			}
		}
	}
   closedir($pointeur);
}

function enleveaccents($string){
       $string = strtr($string,
	'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
	'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
    return $string;
    }

function validateString($element, $require_content = true){
	return (!is_string($element)) ? false : ($require_content && $element == '' ? false : true);
}

function convertSymbols2Entities($string){
	static $symbols = 
		array(
			'‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '‘', '’', '“', '”', 
			'•', '–', '—', '˜', '™', 'š', '›', 'œ', 'Ÿ', '€', 'Æ', 'Á', 'Â', 'À', 'Å', 
			'Ã', 'Ä', 'Ç', 'Ð', 'É', 'Ê', 'È', 'Ë', 'Í', 'Î', 'Ì', 'Ï', 'Ñ', 'Ó', 'Ô', 
			'Ò', 'Ø', 'Õ', 'Ö', 'Þ', 'Ú', 'Û', 'Ù', 'Ü', 'Ý', 'á', 'â', 'æ', 'à', 'å', 
			'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ð', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 
			'ò', 'ø', 'õ', 'ö', 'ß', 'þ', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', '¡', '£', '¤', 
			'¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', 
			'´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', '×', '÷', '¢', 
			'…', 'µ');
	static $entities = 
		array(
			'&#8218;',  '&#402;',   '&#8222;',  '&#8230;',  '&#8224;',  '&#8225;',  '&#710;', 
			'&#8240;',  '&#352;',   '&#8249;',  '&#338;',   '&#8216;',  '&#8217;',  '&#8220;', 
			'&#8221;',  '&#8226;',  '&#8211;',  '&#8212;',  '&#732;',   '&#8482;',  '&#353;', 
			'&#8250;',  '&#339;',   '&#376;',   '&#8364;',  '&aelig;',  '&aacute;', '&acirc;',  
			'&agrave;', '&aring;',  '&atilde;', '&auml;',   '&ccedil;', '&eth;',    '&eacute;', 
			'&ecirc;',  '&egrave;', '&euml;',   '&iacute;', '&icirc;',  '&igrave;', '&iuml;',   
			'&ntilde;', '&oacute;', '&ocirc;',  '&ograve;', '&oslash;', '&otilde;', '&ouml;',   
			'&thorn;',  '&uacute;', '&ucirc;',  '&ugrave;', '&uuml;',   '&yacute;', '&aacute;', 
			'&acirc;',  '&aelig;',  '&agrave;', '&aring;',  '&atilde;', '&auml;',   '&ccedil;', 
			'&eacute;', '&ecirc;',  '&egrave;', '&eth;',    '&euml;',   '&iacute;', '&icirc;',  
			'&igrave;', '&iuml;',   '&ntilde;', '&oacute;', '&ocirc;',  '&ograve;', '&oslash;', 
			'&otilde;', '&ouml;',   '&szlig;',  '&thorn;',  '&uacute;', '&ucirc;',  '&ugrave;', 
			'&uuml;',   '&yacute;', '&yuml;',   '&iexcl;',  '&pound;',  '&curren;', '&yen;',    
			'&brvbar;', '&sect;',   '&uml;',    '&copy;',   '&ordf;',   '&laquo;',  '&not;',    
			'&shy;',    '&reg;',    '&macr;',   '&deg;',    '&plusmn;', '&sup2;',   '&sup3;',   
			'&acute;',  '&micro;',  '&para;',   '&middot;', '&cedil;',  '&sup1;',   '&ordm;',   
			'&raquo;',  '&frac14;', '&frac12;', '&frac34;', '&iquest;', '&times;',  '&divide;', 
			'&cent;',   '...',      '&micro;');

	if (validateString($string, false)) {
		return str_replace($symbols, $entities, $string);
	} else {
		return $string;
	}
}

function Code2String($string){
	
	$string = ereg_replace('\(titre\)','<h2>',$string);
	$string = ereg_replace('\(@titre\)','</h2>',$string);
	
	$string = ereg_replace('\(soustitre\)','<h3>',$string);
	$string = ereg_replace('\(@soustitre\)','</h3>',$string);
	
	$string = ereg_replace('\(souligne\)','<u>',$string);
	$string = ereg_replace('\(@souligne\)','</u>',$string);
	
	$string = ereg_replace('\(surligne\)', '<span style="background:#caa; color:white;">', $string);
	$string = ereg_replace('\(@surligne\)', '</span>', $string);
	
	$string = ereg_replace('\(gras\)', '<b>', $string);
	$string = ereg_replace('\(@gras\)', '</b>', $string);
	
	$string = ereg_replace('\(ita\)', '<i>', $string);
	$string = ereg_replace('\(@ita\)', '</i>', $string);
	return $string;
}

function RemoveCode($string){
	
	$string = ereg_replace('\(titre\)','',$string);
	$string = ereg_replace('\(@titre\)','',$string);
	
	$string = ereg_replace('\(soustitre\)','',$string);
	$string = ereg_replace('\(@soustitre\)','',$string);
	
	$string = ereg_replace('\(souligne\)','',$string);
	$string = ereg_replace('\(@souligne\)','',$string);
	
	$string = ereg_replace('\(surligne\)', '', $string);
	$string = ereg_replace('\(@surligne\)', '', $string);
	
	$string = ereg_replace('\(gras\)', '', $string);
	$string = ereg_replace('\(@gras\)', '', $string);
	
	$string = ereg_replace('\(ita\)', '', $string);
	$string = ereg_replace('\(@ita\)', '', $string);
	return $string;
}
function note2code($target,$string){
	$array = read_note ($target);
	$link = NULL;
	$links = "";
	$texte="";
	for ($i=0; $i != sizeOf($array); $i++){
		$links = "";
		$num = $array[$i][0];
		$texte = $array[$i][1];
		$note =  $array[$i][2];
		if($array[$i][3] != NULL){
			$list = $array[$i][3];
			$link = split("::",$list);
			$links = array2links ($array,$link);
		}
		$textes[$i] = "$texte";
		$bulle [$i] = bulle2code($target,$texte,$note,$list,$num);
	}
	$string = str_replace($textes,$bulle,$string);
	
	for ($i=0;$i!=sizeOf($array);$i++){
		$num_id = $array[$i][0];
		$num_id++;$num_id--;
		$texte2change = " [idnote$num_id] ";
		$string = str_replace($texte2change,$textes[$i],$string);
	}
	return $string;	
}

function bulle2code($target,$texte,$noted,$links,$num){
	$note = "$noted|$links|$num";
	$bulle= "(note)$texte|$note(@note)";
	return $bulle;
}

?>
